package ahut2020.exp06;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

//统计一个文件夹中所有的java代码的行数（包括子目录）
public class P08CounterCodeLines {
    public static void main(String[] args) {
        File file = new File("app");
        System.out.println(file.exists());
        int lines = countCodeLines(file);
        System.out.println(lines);
    }

    private static int countCodeLines(File file) {
        if (file.isFile()) {
            if (!file.getName().endsWith(".java")) return 0;
            int lines = 0;
            try {
                Scanner cin = new Scanner(file);
                while (cin.hasNextLine()) {
                    String line = cin.nextLine();
                    if (line.trim().isEmpty()) continue;
                    ++lines;
                }
            } catch (IOException e) {

            }
            System.err.println(file.getAbsolutePath() + " " + lines + " lines");
            return lines;


        }
        String[] sfiles = file.list();
        int lines = 0;
        for (String sfile : sfiles) {

            lines += countCodeLines(new File(file, sfile));
        }
        return lines;
    }
}
